package com.k2091.washcar.service.impl;

import com.k2091.washcar.entity.*;
import com.k2091.washcar.mapper.*;
import com.k2091.washcar.service.ICarOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2021-06-01
 */
@Service
public class CarOrderServiceImpl extends ServiceImpl<CarOrderMapper, CarOrder> implements ICarOrderService {

    @Autowired
    private CleanerMapper cleanerMapper;
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private UsersCarMapper usersCarMapper;
    @Autowired
    private CarOrderMapper carOrderMapper;

    @Override
    public List<CarOrder> selectCarOrder(FormInline formInline) {
        List<CarOrder> carOrderList=carOrderMapper.selectCarOrder(formInline);
        Users users = null;
        Cleaner cleaner = null;
        UsersCar usersCar = null;
        for (CarOrder order:carOrderList) {
            users=usersMapper.selectById(order.getUserId());
            if (users!=null){
                order.setUserName(users.getName());
                order.setUserMobile(users.getMobile());
            }
            cleaner=cleanerMapper.selectById(order.getCleanerId());
            if (cleaner!=null){
                order.setCleanerName(cleaner.getName());
                order.setCleanerMobile(cleaner.getPhoneNumber());
            }
            usersCar=usersCarMapper.selectById(order.getCarId());
            if (usersCar!=null){
                order.setCar(usersCar.getNumber());
                order.setColor(usersCar.getColor());
            }
        }
        System.out.println(carOrderList);
        return carOrderList;
    }

    @Override
    public List<CarOrder> selectUnreceiveOrder() {
        return carOrderMapper.selectOrder(null,0,null);
    }

    @Override
    public int addOrder(CarOrder carOrder) {
        return carOrderMapper.insert(carOrder);
    }

    @Override
    public int updateOrder(Long orderId, Long cleanId) {
        CarOrder carOrder = carOrderMapper.selectById(orderId);
        if(carOrder.getStatus() == 0) {
            carOrder.setCleanerId(cleanId);
            carOrder.setStatus(1);
            Cleaner cleaner = cleanerMapper.selectById(cleanId);
            cleaner.setOrderStatus(0);
            cleaner.setOrderNumber(carOrder.getId());
            cleanerMapper.updateById(cleaner);
            return carOrderMapper.updateById(carOrder);
        }else {
            return 0;   //失败返回0
        }
    }

    @Override
    public int updateOrderStatus(Long orderId, Long cleanId) {
        CarOrder carOrder = carOrderMapper.selectById(orderId);
        if(carOrder.getStatus() == 1 ) {
            carOrder.setStatus(2);
            Cleaner cleaner = cleanerMapper.selectById(cleanId);
            cleaner.setOrderStatus(1);
            cleanerMapper.updateById(cleaner);
            return carOrderMapper.updateById(carOrder);
        }else {
            return 0;   //失败返回0
        }
    }

    @Override
    public int orderFinish(Long orderId,String rank) {
        CarOrder carOrder = carOrderMapper.selectById(orderId);
        if(carOrder.getStatus() == 1) {
            if (rank != null) {
                carOrder.setStatus(3);
                carOrder.setRank(rank);
            } else {
                carOrder.setStatus(2);
            }
            Cleaner cleaner = cleanerMapper.selectById(carOrder.getCleanerId());
            cleaner.setBalance(cleaner.getBalance().add(carOrder.getPayAmount().multiply(BigDecimal.valueOf(0.3))));
            cleanerMapper.updateById(cleaner);
            return carOrderMapper.updateById(carOrder);
        }else {
            return 0;    //失败返回0
        }
    }

    @Override
    public List<CarOrder> selectOrderByCleaner(Long cleanerId) {
        return carOrderMapper.selectOrder(null,null,cleanerId);
    }

    @Override
    public int userRank(String rank,Long orderId) {
        CarOrder carOrder = carOrderMapper.selectById(orderId);
        carOrder.setRank(rank);
        carOrder.setStatus(3);
        return carOrderMapper.updateById(carOrder);
    }

    @Override
    public List<CarOrder> userUnRank(Long userId) {
        return carOrderMapper.selectOrder(userId,2,null);
    }

    @Override
    public List<CarOrder> selectAllOrder(Long userId) {
        return carOrderMapper.selectOrder(userId,null,null);
    }


}
